<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/9/15
 * Time: 下午3:28
 */

class Singleton {
    private static $ingleton;

    public static function getInstance()
    {
        if (null === static::$ingleton) {
            static::$ingleton = new Singleton();
        }
        return static::$ingleton;
    }

    /**
     * 构造函数私有，不允许在外部实例化
     * final 防止子类改写父类方法的权限
     */
    final private function __construct()
    {
    }

    /**
     * 防止对象实例被克隆
     *
     * @return void
     */
    final private function __clone()
    {
    }

    /**
     * 防止被反序列化
     *
     * @return void
     */
    final private function __wakeup()
    {
    }
}

$a = Singleton::getInstance();
$b = Singleton::getInstance();
if ($a === $b) {
    echo 'a == b' . PHP_EOL;
} else {
    echo 'a != b' . PHP_EOL;
}